package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;

import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.SupplierFullDTO;
import org.springblade.modules.admin.dto.SupplierPageDTO;
import org.springblade.modules.admin.service.ISupplierClassificationService;
import org.springblade.modules.admin.service.ISupplierService;
import org.springblade.modules.admin.vo.SupplierClassificationVO;
import org.springblade.modules.admin.vo.SupplierContactsVO;
import org.springblade.modules.admin.vo.SupplierPageVO;
import org.springframework.web.bind.annotation.*;
import org.springblade.core.boot.ctrl.BladeController;

import java.util.List;

/**
 * 供应商表 控制器
 * @author ysq
 * @since 2023-08-09
 */
@RestController
@AllArgsConstructor
@RequestMapping("admin/supplier")
@Api(value = "供应商表", tags = "供应商表接口")
public class SupplierController extends BladeController {

	private final ISupplierService supplierService;

	private final ISupplierClassificationService supplierClassificationService;

	/**
	 * 新增或修改 供应商分类下拉选择列表
	 */
	@GetMapping("/classificationList")
	@ApiOperationSupport(order = 10)
	@ApiOperation(value = "新增或修改 供应商分类下拉选择列表")
	public R<List<SupplierClassificationVO>> classificationList() {
		return R.data(supplierClassificationService.classificationList());
	}


	/**
	 * 全量新增或修改 供应商表
	 * @param supplierFullDTO 全量新增或修改DTO
	 * @return 是否成功
	 */
	@PostMapping("/addFull")
	@ApiOperationSupport(order = 11)
	@ApiOperation(value = "全量新增或修改 供应商表", notes = "传入supplierFullDTO")
	public R<Boolean> addFull(@RequestBody SupplierFullDTO supplierFullDTO) {
		return R.data(supplierService.addFull(supplierFullDTO));
	}

	/**
	 * 根据当前供应商获取联系人列表，预留校验接口
	 * @return R 联系人列表
	 */
	@GetMapping("/contactsList")
	@ApiOperationSupport(order = 20)
	@ApiOperation(value = "根据当前供应商获取联系人列表，预留校验接口")
	public R<List<SupplierContactsVO>> contactsList(@RequestParam Long supplierId) {
		return R.data(supplierService.contactsList(supplierId));
	}

	/**
	 * 删除 供应商表
	 */
	@PostMapping("/delete")
	@ApiOperationSupport(order = 21)
	@ApiOperation(value = "删除 供应商表", notes = "传入id")
	public R<Boolean> delete(@RequestParam String ids) {
		// 先删除联系人
		supplierService.deleteContacts(Func.toLongList(ids));
		return R.data(supplierService.deleteLogic(Func.toLongList(ids)));
	}

	/**
	 * 自定义分页查询供应商列表
	 * @param supplierPageDTO 供应商Page 查询条件
	 * @return R IPage<List<SupplierPageVO>> 分页结果
	 */
	@PostMapping("/page")
	@ApiOperationSupport(order = 22)
	@ApiOperation(value = "自定义分页查询供应商列表", notes = "传入supplierPageDTO")
	public R<IPage<SupplierPageVO>> page(@RequestBody SupplierPageDTO supplierPageDTO) {
		Query query = new Query();
		query.setCurrent(supplierPageDTO.getCurrent());
		query.setSize(supplierPageDTO.getSize());
		return R.data(supplierService.pageByCondition(Condition.getPage(query),supplierPageDTO));
	}


}
